import { h } from 'vue';
import {useDialog} from "naive-ui"
const dialog = useDialog()
// 闯入删除的ID，名字，和方法
function deletefunc(shaxinfunc,delfunc,itemid,itemname=null) {
    const init=()=>{
        const cc = dialog.warning({
            title:"删除",
            content: ()=>{
                return h("span",{},{default(){return itemname?`确认删除${itemname}？`:"确认删除数据？"}})
            },
            positiveText: '删除',
            negativeText: '取消',
            showIcon:false,
            onPositiveClick: () => {
                cc.loading = true
                delfunc(null,itemid).then(res=>{
                    if(res && res.data.code===0){
                        getsuccess()
                    }else{
                        getfaild(res.data.toString())
                    }
                }).catch(err=>{
                    getfaild(err)
                })
            },
        })
    };
    const getsuccess=()=>{
        const aa = dialog.success({
            title:"删除成功",
            content: ()=>{
                return h("span",{},{default(){return "删除数据成功！"}})
            },
            negativeText: '关闭',
            showIcon:false,
        })
        setTimeout(()=>{
            shaxinfunc()
            aa.destroy()
        },1000)
    }
    const getfaild=(errortext)=>{
        dialog.error({
            title:"删除失败",
            content: ()=>{
                return h("span",{},{default(){return errortext|| "删除错误"}})
            },
            negativeText: '关闭',
            showIcon:false,
        })
    }
    init()
}

function useDelete(){
    return deletefunc
}

export default useDelete